package com.hzhr.iot.collect.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
	//时间转字符串
	public static String dateToStr(Date dateDate, String format) {
		if (dateDate == null) return null;
	   SimpleDateFormat sdf = new SimpleDateFormat(format);
	   String dateString = sdf.format(dateDate);
	   return dateString;
	}
	
	//时间转字符串(format默认为“yyyy-MM-dd HH:mm:ss”)
	public static String dateToStr(Date dateDate) {
		if (dateDate == null) return null;
	   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   String dateString = sdf.format(dateDate);
	   return dateString;
	}
	
	//字符串转时间
	public static Date strToDate(String dateStr, String format) {
		if (dateStr == null) return null;
		try {
			SimpleDateFormat sdf  =   new  SimpleDateFormat( format ); 
			return sdf.parse(dateStr);
		} catch (Exception e) {
			try {
				SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM-dd" ); 
				return sdf.parse(dateStr);
			} catch (Exception e2) {
				try {
					SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM" ); 
					return sdf.parse(dateStr);
				} catch (Exception e3) {
					try {
						SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM" ); 
						return sdf.parse(dateStr);
					} catch (Exception e4) {
						try {
							SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy" ); 
							return sdf.parse(dateStr);
						} catch (Exception e5) {
							return null;
						}
					}
				}
			}
		}
	}
	
	//字符串转时间
	public static Date strToDate(String dateStr) {
		if (dateStr == null) return null;
		
		SimpleDateFormat sdf;
		try {
			sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
			return sdf.parse(dateStr);
		} catch (Exception e) {
			try {
				sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm" ); 
				return sdf.parse(dateStr);
			} catch (Exception e1) {
				try {
					sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH" ); 
					return sdf.parse(dateStr);
				} catch (Exception e2) {
					try {
						sdf  =   new  SimpleDateFormat( "yyyy-MM-dd" ); 
						return sdf.parse(dateStr);
					} catch (Exception e3) {
						try {
							sdf  =   new  SimpleDateFormat( "yyyy-MM" ); 
							return sdf.parse(dateStr);
						} catch (Exception e4) {
							try {
								sdf  =   new  SimpleDateFormat( "yyyy" ); 
								return sdf.parse(dateStr);
							} catch (Exception e5) {
								return null;
							}
						}
					}
				}
			}
		}
	}
	
	//格式化时间字符串
	public static String formatDateStr(String dateStr) {
		if (dateStr == null) return null;
		
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf;
		try {
			sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
			Date date = sdf.parse(dateStr);
			sdf = new  SimpleDateFormat( format ); 
			String result = sdf.format(date);
			return result;
		} catch (Exception e) {
			try {
				sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm" ); 
				Date date = sdf.parse(dateStr);
				sdf = new  SimpleDateFormat( format ); 
				String result = sdf.format(date);
				return result;
			} catch (Exception e1) {
				try {
					sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH" ); 
					Date date = sdf.parse(dateStr);
					sdf = new  SimpleDateFormat( format ); 
					String result = sdf.format(date);
					return result;
				} catch (Exception e2) {
					try {
						sdf  =   new  SimpleDateFormat( "yyyy-MM-dd" ); 
						Date date = sdf.parse(dateStr);
						sdf = new  SimpleDateFormat( format ); 
						String result = sdf.format(date);
						return result;
					} catch (Exception e3) {
						try {
							sdf  =   new  SimpleDateFormat( "yyyy-MM" ); 
							Date date = sdf.parse(dateStr);
							sdf = new  SimpleDateFormat( format ); 
							String result = sdf.format(date);
							return result;
						} catch (Exception e4) {
							try {
								sdf  =   new  SimpleDateFormat( "yyyy" ); 
								Date date = sdf.parse(dateStr);
								sdf = new  SimpleDateFormat( format ); 
								String result = sdf.format(date);
								return result;
							} catch (Exception e5) {
								return null;
							}
						}
					}
				}
			}
		}	
	}
	
	//格式化时间字符串
	public static String formatDateStr(String dateStr, String format) {
		if (dateStr == null) return null;
		
		SimpleDateFormat sdf;
		try {
			sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
			Date date = sdf.parse(dateStr);
			sdf = new  SimpleDateFormat( format ); 
			String result = sdf.format(date);
			return result;
		} catch (Exception e) {
			try {
				sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm" ); 
				Date date = sdf.parse(dateStr);
				sdf = new  SimpleDateFormat( format ); 
				String result = sdf.format(date);
				return result;
			} catch (Exception e1) {
				try {
					sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH" ); 
					Date date = sdf.parse(dateStr);
					sdf = new  SimpleDateFormat( format ); 
					String result = sdf.format(date);
					return result;
				} catch (Exception e2) {
					try {
						sdf  =   new  SimpleDateFormat( "yyyy-MM-dd" ); 
						Date date = sdf.parse(dateStr);
						sdf = new  SimpleDateFormat( format ); 
						String result = sdf.format(date);
						return result;
					} catch (Exception e3) {
						try {
							sdf  =   new  SimpleDateFormat( "yyyy-MM" ); 
							Date date = sdf.parse(dateStr);
							sdf = new  SimpleDateFormat( format ); 
							String result = sdf.format(date);
							return result;
						} catch (Exception e4) {
							try {
								sdf  =   new  SimpleDateFormat( "yyyy" ); 
								Date date = sdf.parse(dateStr);
								sdf = new  SimpleDateFormat( format ); 
								String result = sdf.format(date);
								return result;
							} catch (Exception e5) {
								return null;
							}
						}
					}
				}
			}
		}	
	}
	
	//格式化时间字符串列表
	public static List<String> formatDateStrList(List<String> dateStrList, String format) throws Exception {
		if (dateStrList == null) return null;
		
		List<String> result = new ArrayList<>();
		for (String dateStr : dateStrList) {
			result.add(formatDateStr(dateStr, format));
		}
		return result;
	}
	
	//date2比date1多的天数
	//比较时间大小
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
       int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年            
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            
            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
    
  //获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    //获取昨天的开始时间
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取昨天的结束时间
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }
    //获取明天的结束时间
    public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }
    //获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }
    //获取本周的结束时间
    public static Date getEndDayOfWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());  
        cal.add(Calendar.DAY_OF_WEEK, 6); 
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }
    //获取本月的开始时间
     public static Date getBeginDayOfMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getNowYear(), getNowMonth() - 1, 1);
            return getDayStartTime(calendar.getTime());
        }
    //获取本月的结束时间
     public static Date getEndDayOfMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.set(getNowYear(), getNowMonth() - 1, 1);
            int day = calendar.getActualMaximum(5);
            calendar.set(getNowYear(), getNowMonth() - 1, day);
            return getDayEndTime(calendar.getTime());
        }
     //获取本年的开始时间
     public static Date getBeginDayOfYear() {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, getNowYear());
            // cal.set
            cal.set(Calendar.MONTH, Calendar.JANUARY);
            cal.set(Calendar.DATE, 1);

            return getDayStartTime(cal.getTime());
        }
     //获取本年的结束时间
     public static Date getEndDayOfYear() {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, getNowYear());
            cal.set(Calendar.MONTH, Calendar.DECEMBER);
            cal.set(Calendar.DATE, 31);
            return getDayEndTime(cal.getTime());
        }
    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }
    //获取某个日期的开始时间
    public static String getDayStartTimeStr(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
        String startTimeStr = dateToStr(new Date(timestamp.getTime()));
        return startTimeStr;
    }
    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }
    //获取今年是哪一年
     public static Integer getNowYear() {
             Date date = new Date();
            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
            gc.setTime(date);
            return Integer.valueOf(gc.get(1));
        }
     //获取本月是哪一月
     public static int getNowMonth() {
             Date date = new Date();
            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
            gc.setTime(date);
            return gc.get(2) + 1;
        }
     //两个日期相减得到的天数
     public static int getDiffDays(Date beginDate, Date endDate) {

            if (beginDate == null || endDate == null) {
                throw new IllegalArgumentException("getDiffDays param is null!");
            }

            long diff = (endDate.getTime() - beginDate.getTime())
                    / (1000 * 60 * 60 * 24);

            int days = new Long(diff).intValue();

            return days;
        }
    //两个日期相减得到的毫秒数
     public static long dateDiff(Date beginDate, Date endDate) {
            long date1ms = beginDate.getTime();
            long date2ms = endDate.getTime();
            return date2ms - date1ms;
        }
     
   //两个日期相减得到的秒数
     public static long dateDiffSeconds(Date beginDate, Date endDate) {
    	 	long milliseconds = dateDiff(beginDate, endDate);
    	 	long seconds = milliseconds / 1000;
            return seconds;
        }
     
     //获取两个日期中的最大日期
     public static Date max(Date beginDate, Date endDate) {
            if (beginDate == null) {
                return endDate;
            }
            if (endDate == null) {
                return beginDate;
            }
            if (beginDate.after(endDate)) {
                return beginDate;
            }
            return endDate;
        }
     //获取两个日期中的最小日期
     public static Date min(Date beginDate, Date endDate) {
            if (beginDate == null) {
                return endDate;
            }
            if (endDate == null) {
                return beginDate;
            }
            if (beginDate.after(endDate)) {
                return endDate;
            }
            return beginDate;
        }
     //返回某月该季度的第一个月
     public static Date getFirstSeasonDate(Date date) {
             final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int sean = SEASON[cal.get(Calendar.MONTH)];
            cal.set(Calendar.MONTH, sean * 3 - 3);
            return cal.getTime();
        }
     //返回某个日期下几天的日期
     public static Date getNextDay(Date date, int i) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(date);
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
            return cal.getTime();
        }
     //返回某个日期前几天的日期
     public static Date getFrontDay(Date date, int i) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(date);
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
            return cal.getTime();
        }
     //获取某年某月到某年某月按天的切片日期集合（间隔天数的日期集合）
     public static List<List<Date>> getTimeList(int beginYear, int beginMonth, int endYear,
                int endMonth, int k) {
    	 	List<List<Date>> list = new ArrayList<>();
            if (beginYear == endYear) {
                for (int j = beginMonth; j <= endMonth; j++) {
                    list.add(getTimeList(beginYear, j, k));

                }
            } else {
                {
                    for (int j = beginMonth; j < 12; j++) {
                        list.add(getTimeList(beginYear, j, k));
                    }

                    for (int i = beginYear + 1; i < endYear; i++) {
                        for (int j = 0; j < 12; j++) {
                            list.add(getTimeList(i, j, k));
                        }
                    }
                    for (int j = 0; j <= endMonth; j++) {
                        list.add(getTimeList(endYear, j, k));
                    }
                }
            }
            return list;
        }
     //获取某年某月按天切片日期集合（某个月间隔多少天的日期集合）
     public static List<Date> getTimeList(int beginYear, int beginMonth, int k) {
            List<Date> list = new ArrayList<>();
            Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
            int max = begincal.getActualMaximum(Calendar.DATE);
            for (int i = 1; i < max; i = i + k) {
                list.add(begincal.getTime());
                begincal.add(Calendar.DATE, k);
            }
            begincal = new GregorianCalendar(beginYear, beginMonth, max);
            list.add(begincal.getTime());
            return list;
        }
     
     
     /**
      * 获取时间段中的小时(左闭右闭)
      * @title getHourBetween
      * @description
      * @param minDate
      * @param maxDate
      * @return
      * @author 48310
      * @date 2018年3月3日
      * @see 
      *
      */
     public static List<String> getHourBetween(String minDate, String maxDate) {
    	 try {
    		 ArrayList<String> result = new ArrayList<String>();  
        	 String format = "yyyy-MM-dd HH";
             SimpleDateFormat sdf = new SimpleDateFormat(format);
       
             Calendar min = Calendar.getInstance();  
             Calendar max = Calendar.getInstance();  
             min.setTime(sdf.parse(minDate));  
             min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), min.get(Calendar.DAY_OF_MONTH), min.get(Calendar.HOUR_OF_DAY), 1);  
       
             max.setTime(sdf.parse(maxDate));  
             max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), max.get(Calendar.DAY_OF_MONTH), max.get(Calendar.HOUR_OF_DAY), 2);  
             Calendar curr = min;  
             while (curr.before(max)) {  
              result.add(sdf.format(curr.getTime()));  
              curr.add(Calendar.HOUR_OF_DAY, 1);  
             }  
             return result; 
		} catch (Exception e) {
			return new ArrayList<>();
		}
     }
     
     //获取时间段中的日
     public static List<String> getDayBetween(String minDate, String maxDate) throws Exception {
    	 ArrayList<String> result = new ArrayList<String>();  
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   
         Calendar min = Calendar.getInstance();  
         Calendar max = Calendar.getInstance();  
         min.setTime(sdf.parse(minDate));  
         min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), min.get(Calendar.DAY_OF_MONTH));  
   
         max.setTime(sdf.parse(maxDate));  
         max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), max.get(Calendar.DAY_OF_MONTH) + 1);  
   
         Calendar curr = min;  
         while (curr.before(max)) {  
          result.add(sdf.format(curr.getTime()));  
          curr.add(Calendar.DAY_OF_YEAR, 1);  
         }  
   
         return result; 
     }
     
     //获取时间段中的月
     public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {  
         ArrayList<String> result = new ArrayList<String>();  
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
   
         Calendar min = Calendar.getInstance();  
         Calendar max = Calendar.getInstance();  
   
         min.setTime(sdf.parse(minDate));  
         min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);  
   
         max.setTime(sdf.parse(maxDate));  
         max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);  
   
         Calendar curr = min;  
         while (curr.before(max)) {  
          result.add(sdf.format(curr.getTime()));  
          curr.add(Calendar.MONTH, 1);  
         }  
   
         return result;  
     } 
     
     //获取时间段中的年
     public static List<String> getYearBetween(String minDate, String maxDate) throws Exception {  
         ArrayList<String> result = new ArrayList<String>();  
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
   
         Calendar min = Calendar.getInstance();  
         Calendar max = Calendar.getInstance();  
   
         min.setTime(sdf.parse(minDate));  
         min.set(min.get(Calendar.YEAR), 1, 1);  
   
         max.setTime(sdf.parse(maxDate));  
         max.set(max.get(Calendar.YEAR), 1, 2);  
   
         Calendar curr = min;  
         while (curr.before(max)) {  
          result.add(sdf.format(curr.getTime()));  
          curr.add(Calendar.YEAR, 1);  
         }  
   
         return result;  
     } 
     
     
     //日期字符串转换为小时集合
     public static List<String> dateToHour(String dateStr) throws Exception {
 		String format = "yyyy-MM-dd";
 		Date date = strToDate(dateStr, format);
 		dateStr = dateToStr(date, format);
 		List<String> hourList = new ArrayList<>();
 		for (int i = 0; i < 24; i++) {
 			String hour;
 			if (i < 10) {
 				hour = "0" + i + ":00:00";
 			} else {
 				hour = i + ":00:00";
 			}
 			hourList.add(dateStr + " " + hour);
 		}
 		return hourList;
 	}
     
    //日期加n天
 	public static String addDay(String dateStr, int n, String format) {    
         try {    
                  SimpleDateFormat sdf = new SimpleDateFormat(format);    
    
                  Calendar cd = Calendar.getInstance();    
                  cd.setTime(sdf.parse(dateStr));    
                  cd.add(Calendar.DATE, n);//增加一天    
                  //cd.add(Calendar.MONTH, n);//增加一个月    
    
                  return sdf.format(cd.getTime());    
         
              } catch (Exception e) { 
             	 e.printStackTrace();
                  return null;    
              }    
      }  
 	
 	//日期加n天
 	public static Date addDay(Date date, int n) {    
         try {    
        	 	  String format = "yyyy-MM-dd HH:mm:ss";
        	 	  String dateStr = dateToStr(date);
                  SimpleDateFormat sdf = new SimpleDateFormat(format);    
    
                  Calendar cd = Calendar.getInstance();    
                  cd.setTime(sdf.parse(dateStr));    
                  cd.add(Calendar.DATE, n);//增加一天    
                  //cd.add(Calendar.MONTH, n);//增加一个月    
    
                  return cd.getTime();    
         
              } catch (Exception e) { 
             	 e.printStackTrace();
                  return null;    
              }    
      }  
     
 	//日期加n个月
 	 	public static Date addMonth(Date date, int n) {    
 	         try {    
 	        	 	  String format = "yyyy-MM-dd HH:mm:ss";
 	        	 	  String dateStr = dateToStr(date);
 	                  SimpleDateFormat sdf = new SimpleDateFormat(format);    
 	    
 	                  Calendar cd = Calendar.getInstance();    
 	                  cd.setTime(sdf.parse(dateStr));    
 	                  cd.add(Calendar.MONTH, n);//增加一天    
 	                  //cd.add(Calendar.MONTH, n);//增加一个月    
 	    
 	                  return cd.getTime();    
 	         
 	              } catch (Exception e) { 
 	             	 e.printStackTrace();
 	                  return null;    
 	              }    
 	      }  
 	 	
 	     //日期加n秒
 	 	public static String addSec(String dateStr, int n, String format) {    
 	 		try {    
                SimpleDateFormat sdf = new SimpleDateFormat(format);    
  
                Calendar cd = Calendar.getInstance();    
                cd.setTime(sdf.parse(dateStr));    
                cd.add(Calendar.SECOND, n);//增加一秒    
                //cd.add(Calendar.MONTH, n);//增加一个月    
  
                return sdf.format(cd.getTime());    
       
            } catch (Exception e) { 
           	 e.printStackTrace();
                return null;    
            }   
 	      }  	
 	 	
 	 //日期加n秒
 	 	public static String addSec(Date date, int n, String format) {    
 	 		try {    
 	 			String dateStr = dateToStr(date, format);
                SimpleDateFormat sdf = new SimpleDateFormat(format);    
  
                Calendar cd = Calendar.getInstance();    
                cd.setTime(sdf.parse(dateStr));    
                cd.add(Calendar.SECOND, n);//增加一秒    
                //cd.add(Calendar.MONTH, n);//增加一个月    
  
                return sdf.format(cd.getTime());    
       
            } catch (Exception e) { 
           	 e.printStackTrace();
                return null;    
            }   
 	      }  
 	
     //日期加n个单位时间
 	public static String addTime(String dateStr, int n, String format, int unit) {    
         try {    
                  SimpleDateFormat sdf = new SimpleDateFormat(format);    
    
                  Calendar cd = Calendar.getInstance();    
                  cd.setTime(strToDate(dateStr, format));   
                  cd.add(unit, n);
                  //cd.add(Calendar.HOUR, n);//增加一小时
                  //cd.add(Calendar.MONTH, n);//增加一个月    
    
                  return sdf.format(cd.getTime());    
         
              } catch (Exception e) { 
             	 e.printStackTrace();
                  return null;    
              }    
      }  
 	
 	//获取指定日期所在星期的所有日期
 	public static List<String> dateToWeek(String dateStr) throws Exception {
		String format = "yyyy-MM-dd";
		Date date = strToDate(dateStr, format);
		Calendar cal = Calendar.getInstance();  
	     cal.setTime(date);  
	     // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了  
	     int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
	     if (1 == dayWeek) {  
	        cal.add(Calendar.DAY_OF_MONTH, -1);  
	     }  
	     // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期  
	     // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  
	     cal.setFirstDayOfWeek(Calendar.MONDAY);  
	     // 获得当前日期是一个星期的第几天  
	     int day = cal.get(Calendar.DAY_OF_WEEK);  
	     // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
	     cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
	     
	     List<String> dateList = new ArrayList<>();
	     SimpleDateFormat sdf = new SimpleDateFormat(format);   
	     String imptimeBegin = sdf.format(cal.getTime());  
	     // System.out.println("所在周星期一的日期：" + imptimeBegin);
	     dateList.add(imptimeBegin);
	     for (int i = 1; i <= 6; i++) { 
	    	 String nextDate = addDay(imptimeBegin, i, "yyyy-MM-dd");
	    	 dateList.add(nextDate);
	     }
	     return dateList;
	}
 	
 	//获取指定日期所在月的所有日期
 	public static List<String> monthToDay(String monthStr) throws Exception {
		String format = "yyyy-MM";
		Date date = strToDate(monthStr, format);
		monthStr = dateToStr(date, format);
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		int count = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		List<String> dayList = new ArrayList<>();
		for(int i = 1; i <= count; i++) {
			String day;
			if (i < 10) {
				day = "-0" + i;
			} else {
				day = "-" + i;
			}
			dayList.add(monthStr + day);
		}
		return dayList;
	}
 	
 	//获取指定日期所在年的所有月份
 	public static List<String> yearToMonth(String yearStr) throws Exception {
		String format = "yyyy";
		Date date = strToDate(yearStr, format);
		yearStr = dateToStr(date, format);
		List<String> monthList = new ArrayList<>();
		for (int i = 1; i <= 12; i++) {
			String month;
			if (i < 10) {
				month = "-0" + i;
			} else {
				month = "-" + i;
			}
			monthList.add(yearStr + month);
		}
		return monthList;
	}
 	
 	//判断时间是否满足指定格式
 	public static boolean checkDateStrFormat(String dateStr, String format) {
 		try {
 			SimpleDateFormat sdf  =   new  SimpleDateFormat( format ); 
 			sdf.parse(dateStr);
			return true;
		} catch (Exception e) {
			return false;
		}
 	}

 	
 	public static String getMonthFirstDayByMonthStr(String month) {
 		String result = month + "-01";
 		return result;
 	}
 	
 	public static String getMonthFirstDay(Date month) {
 		String monthStr = dateToStr(month, "yyyy-MM");
 		return getMonthFirstDayByMonthStr(monthStr);
 	}
 	
 	public static String getMonthLastDayByMonthStr(String month) {
 		String nextMonth = addTime(month, 1, "yyyy-MM", Calendar.MONTH);
 		String nextMonthFirestDay = getMonthFirstDayByMonthStr(nextMonth);
 		String result = addTime(nextMonthFirestDay, -1, "yyyy-MM-dd", Calendar.DAY_OF_YEAR);
 		return result;
 	}
 	
 	public static String getMonthLastDay(Date month) {
 		String monthStr = dateToStr(month, "yyyy-MM");
 		return getMonthLastDayByMonthStr(monthStr);
 	}
 	
 	public static String getLastMonthFirstDayByMonthStr(String month) {
 		String lastMonthFirstDay = addTime(month, -1, "yyyy-MM-dd", Calendar.MONTH);
 		return lastMonthFirstDay;
 	}
 	
 	public static String getNextMonthFirstDayByMonthStr(String month) {
 		String nextMonth = addTime(month, 1, "yyyy-MM", Calendar.MONTH);
 		String result = getMonthFirstDayByMonthStr(nextMonth);
 		return result;
 	}
 	
 	public static String getNextMonthFirstDay(Date month) {
 		String monthStr = dateToStr(month, "yyyy-MM");
 		return getNextMonthFirstDayByMonthStr(monthStr);
 	}
 	
 	public static String dateToStrForOracleSql(Date date) {
 		String dateStr = dateToStr(date);
 		if (dateStr == null) return null;
 		String result = "to_date('" + dateStr + "', 'yyyy-mm-dd hh24:mi:ss')";
 		return result;
 	}
 	
 	public static String dateToStrForMysqlSql(Date date) {
 		String dateStr = dateToStr(date);
 		if (dateStr == null) return null;
 		String result = "'" + dateStr + "'";
 		return result;
 	}
 	
 	public static Date dateToDay(Date date) {
 		if (date == null) return null;
 		return DateUtil.strToDate(DateUtil.dateToStr(date, "yyyyMMdd"), "yyyyMMdd");
 	}
 	
 	public static Date timestamp2date(Long timestamp) {
 		try {
 			if (timestamp == null) return null;
 			SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
 	 	    String d = format.format(timestamp);  
 	 	    Date date = format.parse(d);
 	 	    return date;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
  	}
 	
 	public static Date timestamp2date(String timestampStr) {
 		try {
 			if (timestampStr == null) return null;
 			Long timestamp = Long.parseLong(timestampStr);
 			SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
 	 	    String d = format.format(timestamp);  
 	 	    Date date = format.parse(d);
 	 	    return date;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
  	}
}
